package com.taomus.mytools.simplec.syntax;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

import com.taomus.mytools.simplec.common.SyntaxTuple;
import com.taomus.mytools.simplec.common.SyntaxTupleList;
import com.taomus.mytools.simplec.common.Token;
import com.taomus.mytools.simplec.common.Word;
import com.taomus.mytools.simplec.common.WordEnum;
import com.taomus.mytools.simplec.utils.ErrorUtils;


public class FAGRS implements IAnalyse{
	private Queue<Token> tkQ;
	private Stack<Token> stack;

	public FAGRS(Queue<Token> tkQ, Stack<Token> stack) {
		this.tkQ = tkQ;
		this.stack = stack;
	}
	
	@Override
	public boolean complier() {
		SyntaxTuple st = null;
		List<SyntaxTuple> stlist = new ArrayList<>();
		SyntaxTuple fanum = new SyntaxTuple(new Token(WordEnum.FAGRSNUM));
		SyntaxTupleList.add(fanum);
		int count = 0;
		while(true){
			Token tk = tkQ.poll();
			if(tk.getCode() == WordEnum.ID || tk.getCode() == WordEnum.NUMBER){
					count += 1;
					stack.push(tk);
			}else{
				switch(tk.getCode()){
				case SPLIT:
					st = new SyntaxTuple(Word.TK_FRAGRS,stack.pop());
					SyntaxTupleList.add(st);
					break;
				case RP:
					st = new SyntaxTuple(Word.TK_FRAGRS,stack.pop());
					SyntaxTupleList.add(st);
					fanum.t1(new Token(WordEnum.NUMBER,String.valueOf(count)));
					return true;
				default:
					ErrorUtils.error(tk + "agrument error");
					return false;
				}
			}
		}
	}
}
